package editingDistance;

import gettree.TreeNode;

import java.util.ArrayList;
import java.util.HashMap;

public class ConstructHashMap {
	public static HashMap<Integer, TreeNode> map ;
	public static HashMap<TreeNode,Integer> inverseMap ;
	public static ArrayList<Integer> LR_keyroots ;
	public static int number ; 
	public static int mostLeft;
	private static int lRoot;

	public static void dowork (TreeNode root){
		map = new HashMap<Integer, TreeNode>();
		number= 0 ; 
		inverseMap = new HashMap<TreeNode, Integer>();
		LR_keyroots  = new ArrayList<Integer>();
		constructHashMap(root);
		lRoot = root.getLi();
		contructLR_keyroots(root);
		LR_keyroots.add(inverseMap.get(root));
	}

	private static void constructHashMap (TreeNode root){
		if (root == null)
			return ; 
		
		for (int i = 0 ; i < root.getChildCount() ; i++){
			constructHashMap(root.getChild(i));
		}
		number ++ ;
		map.put(number, root);
		inverseMap.put(root, number);
		TreeNode ml = root;
		for(;!ml.isLeaf();ml=ml.getChild(0));
		root.setLi(inverseMap.get(ml));
		/*if (root.isLeaf())
		{
			number++;
			map.put(number, root);
			inverseMap.put(root, number);
		}
		else{
			constructHashMap(root.getChild(0));
			
			for (int i = 1 ; i < root.getChildCount();i++){
				
			}
		}*/
	}

	private static void contructLR_keyroots(TreeNode root){
		if (root == null)
			return ;
		if (!root.isLeaf()){
			contructLR_keyroots(root.getChild(0));
			for (int i = 1 ; i < root.getChildCount() ; i++){
				contructLR_keyroots(root.getChild(i));
				LR_keyroots.add(inverseMap.get(root.getChild(i)));

			}
		}
		/*
		for (int i = 1 ; i < root.getChildCount() ; i++){
			contructLR_keyroots(root.getChild(i));
		}
		if (root.getLi() != lRoot)
			LR_keyroots.add(inverseMap.get(root));*/
	}


}
